
###############################################################################
### Did 3G Make Afghan Insurgents Fight More Effectively? 
### Replication files for analyses
### Mehmet Erdem Arslan
###
### *Figure 2*: Comparison of trends in 2G and 3G coverage
###############################################################################

# setwd("your file path here")

library(ggplot2)
library(ggpubr)

load("R/Figure2.RData")

plot1 <- ggplot(afg) + 
  geom_point(aes(log_pop, three, colour = "3G"), size = 0.5, colour = "firebrick2", alpha = 0.3) +
  geom_smooth(aes(log_pop, three, colour = "3G", fill = "3G"), method = "loess", formula = y ~ x, 
              linewidth = 0.5, alpha = 0.5) +
  geom_point(aes(log_pop, two, colour = "2G"), size = 0.5, colour = "skyblue3", alpha = 0.3) + 
  geom_smooth(aes(log_pop, two, colour = "2G", fill = "2G"), method = "loess", formula = y ~ x, 
              linewidth = 0.5, alpha = 0.5) +
  scale_colour_manual("Network type", values = c("3G" = "red4", "2G" = "blue4")) +
  scale_fill_manual("Network type", values = c("3G" = "red3", "2G" = "blue2")) + 
  theme_classic() + theme(legend.position = "bottom") + 
  labs(x = '(log) Population', 
       y = 'Network coverage (%)')

plot2 <- ggplot(afg) + 
  geom_point(aes(nl_mean, three), size = 0.5, colour = "firebrick2", alpha = 0.3) +
  geom_smooth(aes(nl_mean, three), method = "loess", formula = y ~ x, col = "red4", 
              linewidth = 0.5, alpha = 0.5, fill = "red3") +
  geom_point(aes(nl_mean, two), size = 0.5, colour = "skyblue3", alpha = 0.3) + 
  geom_smooth(aes(nl_mean, two), method = "loess", formula = y ~ x, col = "blue4", 
              linewidth = 0.5, alpha = 0.5, fill = "blue2") +
  theme_classic() + 
  labs(x = 'Night lights (mean, calibrated)', 
       y = 'Network coverage (%)')

plot3 <- ggplot(afg) + 
  geom_point(aes(DIST2PROVCAP, three), size = 0.5, colour = "firebrick2", alpha = 0.3) +
  geom_smooth(aes(DIST2PROVCAP, three), method = "loess", formula = y ~ x, col = "red4", 
              linewidth = 0.5, alpha = 0.5, fill = "red3") +
  geom_point(aes(DIST2PROVCAP, two), size = 0.5, colour = "skyblue3", alpha = 0.3) + 
  geom_smooth(aes(DIST2PROVCAP, two), method = "loess", formula = y ~ x, col = "blue4", 
              linewidth = 0.5, alpha = 0.5, fill = "blue2") +
  coord_cartesian(xlim = c(0, 200)) + 
  theme_classic() + 
  labs(x = 'Distance to province capital', 
       y = 'Network coverage (%)')

plot4 <- ggplot(afg) + 
  geom_point(aes(ROAD_DENSITY, three), size = 0.5, colour = "firebrick2", alpha = 0.3) +
  geom_smooth(aes(ROAD_DENSITY, three), method = "loess", formula = y ~ x, col = "red4", 
              linewidth = 0.5, alpha = 0.5, fill = "red3") +
  geom_point(aes(ROAD_DENSITY, two), size = 0.5, colour = "skyblue3", alpha = 0.3) + 
  geom_smooth(aes(ROAD_DENSITY, two), method = "loess", formula = y ~ x, col = "blue4", 
              linewidth = 0.5, alpha = 0.5, fill = "blue2") +
  theme_classic() + 
  labs(x = 'Road density', 
       y = 'Network coverage (%)')


pdf("Outputs/Figure2.pdf", height = 9, width = 8)
ggarrange(plot1, plot2, plot3, plot4, nrow = 2, ncol = 2, 
          common.legend = TRUE, legend.grob = get_legend(plot1), legend = "bottom")
dev.off()

rm(list = ls())

